package com.fau.cm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalKeyValue;
import com.fau.cm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  字典表Action
 * @author yangzj
 * @datetime 2014年10月13日,下午7:45:53
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class DictionaryAction extends PublicAction implements ModelDriven<MedicalKeyValue> {
	
	private static final long serialVersionUID = 472622780661656684L;
	
	private MedicalKeyValue keyVal = new MedicalKeyValue();
	
	/**
	 * @desc 跳转到字典表管理页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:46:53
	 */
	@Action(value = "dic_init", results = {@Result(location = "dictionary_list.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转至字典表管理添加界面 
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:47:53
	 */
	@Action(value = "dictionary_add", results = {@Result(location = "dictionary_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 跳转至字典表管理修改界面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:48:53
	 */
	@Action(value = "dictionary_edit", results = {@Result(location = "dictionary_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询字典信息
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:51:53
	 */
	@Action(value = "dictionary_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getDictionaryService().findDictionaryByPage(admin, keyVal, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 保存
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:52:53
	 */
	@Action(value = "dictionary_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Boolean flag =  this.baseAllService.getDictionaryService().saveDictionary(admin, keyVal);
		if(flag){
			this.map.put("success", true);
		}else{
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 保存父级修改
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:53:53
	 */
	@Action(value = "dictionary_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(keyVal.getId())){
			this.baseAllService.getDictionaryService().editSave(this.keyVal);
			this.map.put("success", true);
		}else{
			this.map.put("error", true);
			}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 删除字典表
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:54:53
	 */
	@Action(value = "dictionary_delete")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(keyVal.getId())){
			this.baseAllService.getDictionaryService().deleteDictionary(this.keyVal);
			this.map.put("success", true);
		}else{
			this.map.put("error", true);
		}
		formatJSON(response, this.map);
		return null;
	}

	/**
	 * @desc 验证字典表，表键
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:58:53
	 */
	@Action(value = "dictionary_ver_key")
	public String verKeyName(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(keyVal.getKeyName())){
			Boolean flag = this.baseAllService.getDictionaryService().verKeyName(admin, keyVal.getKeyName());
			if(flag){
				this.map.put("result", true);
			}else{
				this.map.put("result", false);
			}
		}else{
			this.map.put("error", true);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询字典表类型集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月22日,上午11:41:08
	 */
	@Action(value = "dictionary_key_type_view")
	public String keyTypeView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getDictionaryService().findKeyTypeList();
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 根据字典表类型查集合（话务系统）
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月22日,下午11:11:04
	 */
	@Action(value = "dictionary_cm_key_val_view")
	public String findCmKeyValList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getDictionaryService().findCmKeyValList(admin, SystemConfig.CM_SYSTEM_TYPE, keyVal.getTypeName());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 根据字典表类型查集合(订单系统)
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月22日,下午11:11:04
	 */
	@Action(value = "dictionary_om_key_val_view")
	public String findOmKeyValList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getDictionaryService().findOmKeyValList(admin, SystemConfig.OM_SYSTEM_TYPE, keyVal.getTypeName());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 根据字典表类型查集合(仓储系统)
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月22日,下午11:11:04
	 */
	@Action(value = "dictionary_wm_key_val_view")
	public String findWmKeyValList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getDictionaryService().findWmKeyValList(admin, SystemConfig.WM_SYSTEM_TYPE, keyVal.getTypeName());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 话务任务保存设置初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月23日,下午9:02:18
	 */
	@Action(value = "task_save_init", results = {@Result(location = "task_save_init.jsp")})
	public String taskSaveInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 获取字典表设置
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月23日,下午9:45:59
	 */
	@Action(value = "task_save_get_val")
	public String getTaskSaveVal() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String taskSaveVal = this.baseAllService.getDictionaryService().getTaskSaveVal(admin);
		MedicalKeyValue bean = this.baseAllService.getDictionaryService().getServerIp(admin.getEnterpriseId());
		map.put("taskSaveVal", taskSaveVal);
		map.put("serverIp", bean.getKeyValue());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 修改保存设置
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月23日,下午9:55:45
	 */
	@Action(value = "task_save_edit_val")
	public String editTaskSaveVal() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.baseAllService.getDictionaryService().editTaskSaveVal(admin, keyVal.getKeyName());
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 同步软交换数据
	 * @return
	 * @author kangyj
	 * @datetime 2015年1月9日,上午10:17:00
	 */
	@Action(value = "sync_cti_data")
	public String syncData() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getDictionaryService().syncData(keyVal.getTypeName());
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 设置服务器ip及端口号
	 * @return
	 * @author kangyj
	 * @datetime 2015年3月7日,上午10:58:00
	 */
	@Action(value = "set_server_ip")
	public String setServerIp() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		keyVal.setHospitalId(admin.getEnterpriseId());
		if(StringUtils.isNotBlank(keyVal.getKeyValue())) {
			this.baseAllService.getDictionaryService().setServerIp(keyVal);
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalKeyValue getModel() {
		return keyVal;
	}

}
